﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using DbMapping;
using DbMapping.Common;
using VideoCache.Server.DAL;
using VideoCache.Server.Model;
using VideoCache.Spider;

namespace VideoCache.Server.BLL
{
    public partial class EpisodeBLL
    {
        public static void SearchEpisodes(int albumId)
        {
            AlbumInfo info = AlbumDAL.Find(albumId);

            BaiduAlbumType type = BaiduAlbumType.Movie;
            // 组织百度url
            if (info.TypeId == 2)
            {
                type = BaiduAlbumType.Movie;
            }
            else if (info.TypeId == 3)
            {
                type = BaiduAlbumType.Tv;
            }
            else
            {
                throw new NotImplementedException();
            }

            SpiderAlbum album = SpiderFactory.GetEpisodes(type, info.BaiduId);

            List<EpisodeInfo> episodeList = new List<EpisodeInfo>();
            // 电影直接获取地址
            foreach (SpiderSite siteInfo in album.Sites)
            {
                foreach (SpiderVideo video in siteInfo.Videos)
                {
                    int siteId = TranslateSiteId(siteInfo.SiteName);
                    if (siteId > 0)
                    {
                        EpisodeInfo episodeInfo = new EpisodeInfo();
                        episodeInfo.AlbumId = albumId;
                        episodeInfo.EpisodeIndex = video.Episode;
                        episodeInfo.Url = video.Url;
                        episodeInfo.Name = video.Name;
                        episodeInfo.SiteId = siteId;
                        episodeList.Add(episodeInfo);
                    }
                }
            }

            SaveEpisode(episodeList.ToArray());
        }

        public static void SaveEpisode(EpisodeInfo[] list)
        {
            using (IDbConnection conn = DbAccess.GetConnection())
            {
                conn.Open();
                using (IDbTransaction tran = conn.BeginTransaction())
                {
                    try
                    {
                        foreach (EpisodeInfo info in list)
                        {
                            Conditions cons = new Conditions();
                            cons.Add(new ConditionItem(LinkType.And, EpisodeInfo.AlbumIdField, CompareType.Equal, info.AlbumId));
                            cons.Add(new ConditionItem(LinkType.And, EpisodeInfo.EpisodeIndexField, CompareType.Equal, info.EpisodeIndex));
                            cons.Add(new ConditionItem(LinkType.And, EpisodeInfo.SiteIdField, CompareType.Equal, info.SiteId));
                            if (EpisodeDAL.Exists(cons))
                            {
                                EpisodeInfo episode = EpisodeBLL.Find(cons, null);
                                episode.Url = info.Url;
                                //episode.Token = info.Token;
                                episode.Name = info.Name;
                                episode.ModifyTime = DateTime.Now;
                                EpisodeDAL.Update(tran, episode);
                            }
                            else
                            {
                                info.CreateTime = DateTime.Now;
                                info.ModifyTime = DateTime.Now;
                                EpisodeDAL.Add(tran, info);
                            }
                        }
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        throw new Exception("保存剧集失败。", ex);
                    }
                }
            }
        }

        private static int TranslateSiteId(string sitename)
        {
            Conditions conditions = new Conditions();
            conditions.Add(new ConditionItem(LinkType.And, SiteInfo.DisplayNameField, CompareType.Equal, sitename));

            SiteInfo info = SiteDAL.Find(conditions, new Orders());

            return info == null ? 0 : info.Id;
        }

        public static void Delete(int[] list)
        {
            using (IDbConnection conn = DbAccess.GetConnection())
            {
                conn.Open();
                using (IDbTransaction tran = conn.BeginTransaction())
                {
                    try
                    {
                        foreach (int id in list)
                        {
                            EpisodeDAL.Delete(tran, id);
                        }
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        throw new Exception("删除剧集失败。", ex);
                    }
                }
            }
        }

        public static void SetDownload(int[] list)
        {
            EpisodeDAL.SetDownload(list);
        }
    }
}
